/**
 * 获取柱状图
 *
 * @param title         图表标题，字符串
 * @param legendData    图例数据，一维度数组
 * @param xData         X轴数据，一维数组
 * @param seriesData    图表数据，多维数组
 * @param type          bar/柱状图，line/折线图
 * @param showMaxMin    是否显示最大值和最小值
 * @param showAverage   是否显示平均值
 * @returns object      选项
 */
function getBarOption(title, legendData, xData, seriesData, type, showMaxMin, showAverage) {
    const labelOption = {
        show: true,
        position: 'insideBottom',
        distance: 15,
        align: 'left',
        verticalAlign: 'middle',
        rotate: 90,
        formatter: '{c}  {name|{a}}',
        fontSize: 16,
        rich: {
            name: {}
        }
    };

    const toolbox = {
        show: true,
        orient: 'vertical',
        left: 'right',
        top: 'center',
        feature: {
            mark: {show: true},
            dataView: {show: true, readOnly: false},
            magicType: {show: true, type: ['line', 'bar', 'stack']},
            restore: {show: true},
            saveAsImage: {show: true}
        }
    };

    let series = [];
    for (let i = 0; i < seriesData.length; i++) {
        let data = {
            name: legendData[i],
            type: type,
            smooth: true,
            barGap: 0,
            emphasis: {
                focus: 'series'
            },
            data: seriesData[i],
        };
        if (showMaxMin) {
            data.markPoint = {
                data: [
                    {type: 'max', name: 'Max'},
                    {type: 'min', name: 'Min'}
                ]
            };
        }
        if (showAverage) {
            data.markLine = {
                data: [{type: 'average', name: '平均值'}]
            }
        }
        series.push(data)
    }

    let legend = {
        'orient': 'vertical',
        'left': 'center',
        'bottom': 'bottom',
        'padding': [0, 0, 0, 0],
    };
    legend.data = legendData;
    let selected = {};
    for (let i = 0; i < legendData.length; i++) {
        selected[legendData[i]] = true;
    }
    legend.selected = selected;

    // 指定图表的配置项和数据
    let option = {
        title: {
            text: title,
        },
        // 提示信息
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        // 菜单
        legend: legend,
        // 工具栏
        toolbox: toolbox,
        // X轴数据
        xAxis: [
            {
                type: 'category',
                axisTick: {show: false},
                data: xData,
                axisLabel: {
                    interval: 0, // 代表显示所有x轴标签显示
                    rotate: 45,  // 代表逆时针旋转45度
                }
            }
        ],
        // Y轴数据
        yAxis: [
            {
                type: 'value'
            }
        ],
        // 数据
        series: series,
    };
    return option;
}

/**
 * 获取柱状图-扩展
 *
 * @param title         图表标题，字符串
 * @param legendData    图例数据，一维度数组
 * @param xData         X轴数据，一维数组
 * @param dataOptions   数据选项
 * @returns object      选项
 */
function getBarOptionExt(title, legendData, xData, dataOptions, yaxisOptions) {
    const toolbox = {
        show: true,
        orient: 'vertical',
        left: 'right',
        top: 'center',
        feature: {
            mark: {show: true},
            dataView: {show: true, readOnly: false},
            magicType: {show: true, type: ['line', 'bar', 'stack']},
            restore: {show: true},
            saveAsImage: {show: true}
        }
    };

    let series = [];
    for (let i = 0; i < dataOptions.length; i++) {
        let data = {
            name: legendData[i],
            type: dataOptions[i].type,
            smooth: dataOptions[i].smooth,
            color: dataOptions[i].color,
            barGap: 0,
            emphasis: {
                focus: 'series'
            },
            data: dataOptions[i].data,
            yAxisIndex: dataOptions[i].yaxisIndex,
        };
        if (dataOptions[i].showMaxMin) {
            data.markPoint = {
                data: [
                    {type: 'max', name: 'Max'},
                    {type: 'min', name: 'Min'}
                ]
            };
        }
        if (dataOptions[i].showAverage) {
            data.markLine = {
                data: [{type: 'average', name: '平均值'}]
            }
        }
        series.push(data)
    }

    let legend = {
        'orient': 'vertical',
        'left': 'center',
        'bottom': 'bottom',
        'padding': [0, 0, 0, 0],
    };
    legend.data = legendData;
    let selected = {};
    for (let i = 0; i < legendData.length; i++) {
        selected[legendData[i]] = true;
    }
    legend.selected = selected;

    // 指定图表的配置项和数据
    let option = {
        title: {
            text: title,
        },
        // 提示信息
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        // 菜单
        legend: legend,
        // 工具栏
        toolbox: toolbox,
        // X轴数据
        xAxis: [
            {
                type: 'category',
                axisTick: {show: false},
                data: xData,
                axisLabel: {
                    interval: 0, // 代表显示所有x轴标签显示
                    rotate: 45,  // 代表逆时针旋转45度
                }
            }
        ],
        // Y轴数据
        yAxis: [
            {
                type: 'value',
                splitLine: {
                    show: true
                },
            },
            {
                type: 'value',
                splitLine: {
                    show: false
                },
            }
        ],
        // 数据
        series: series,
    };
    return option;
}

/**
 * 获取饼图
 *
 * @param title         图表标题，字符串
 * @param legendData    菜单数据，一维度数组
 * @param seriesData    图表数据，对象数组，[{ value: 1048, name: 'Search Engine' }]
 * @param legend
 * @returns object      选项
 */
function getPieOption(title, legendData, seriesData, legend = {}) {
    const toolbox = {
        show: true,
        orient: 'vertical',
        left: 'right',
        top: 'center',
        feature: {
            mark: {show: true},
            saveAsImage: {show: false}
        }
    };

    legend.data = legendData;
    let selected = {};
    for (let i = 0; i < legendData.length; i++) {
        selected[legendData[i]] = true;
    }
    legend.selected = selected;
    legend.bottom = 0;

    // 指定图表的配置项和数据
    let option = {
        title: {
            text: title,
        },
        // 提示信息
        tooltip: {
            trigger: 'item',
            formatter: '{b}: {c}({d}%)'
        },
        // 菜单
        legend: legend,
        // 工具栏
        toolbox: toolbox,
        // 饼图数据
        series: [
            {
                name: '',
                type: 'pie',
                radius: '50%',
                data: seriesData,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                },
                label: {
                    normal: {
                        show: true,
                        // 自定义显示格式(b:name, c:value, d:百分比)
                        formatter: '{b}: {c}({d}%)',
                        // outside外部显示  inside内部显示
                        position: "outside"
                    }
                },
            }
        ],
    };
    return option;
}

export default {
    getBarOption,
    getBarOptionExt,
    getPieOption
}
